package com.joysuch.wwyt.workflow.controller;


import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.workflow.WorkFlowConstants;
import com.joysuch.wwyt.workflow.bean.WorkFlowExecuteTaskBean;
import com.joysuch.wwyt.workflow.entity.WorkFlowMenu;
import com.joysuch.wwyt.workflow.entity.WorkFlowProcess;
import com.joysuch.wwyt.workflow.entity.WorkFlowTask;
import com.joysuch.wwyt.workflow.service.WorkFlowHiddenDangerService;
import com.joysuch.wwyt.workflow.service.WorkFlowMenuService;
import com.joysuch.wwyt.workflow.service.WorkFlowProcessService;
import com.joysuch.wwyt.workflow.service.WorkFlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.ActivitiException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/workflow/hidden-danger")
@Api(tags = "业务流程任务- 隐患APP")
@CrossOrigin
public class WorkFlowHiddenDangerController {

    @Autowired
    private WorkFlowHiddenDangerService workFlowHiddenDangerService;

    @Autowired
    private WorkFlowProcessService workFlowProcessService;

    @Autowired
    private WorkFlowTaskService workFlowTaskService;

    @Autowired
    private WorkFlowMenuService workFlowMenuService;



    @PostMapping("/uploadTask")
    @ApiOperation(value = "APP上传任务" )
    public ResultBean uploadTask(@RequestBody WorkFlowTask task){
        try {
            workFlowHiddenDangerService.uploadTask(task);
            return ResultBean.defaultSuccessResult();
        } catch (Exception e) {
            log.error("APP上传任务失败", e);
            throw e;
        }
    }

   /* @GetMapping("/getTaskList")
    @ApiOperation(value = "APP获取任务" )
    public ResultBean getTaskList(@RequestParam String taskName){
       try{
           List<WorkFlowTask> list = workFlowHiddenDangerService.downloadTaskList(taskName);
            return ResultBean.success(list);
       } catch (Exception e) {
           log.error("APP获取任务失败", e);
           throw e;
       }
    }*/

    @GetMapping("/updateAppStatus")
    @ApiOperation(value = "修改APP上传、下载状态" )
    public ResultBean downloadTaskList(@RequestParam Long id, @RequestParam String appStatus){
        try {
            workFlowTaskService.updateAppStatus(id, appStatus);
            return ResultBean.defaultSuccessResult();
        } catch (Exception e) {
            log.error("修改APP上传、下载状态失败", e);
            throw e;
        }
    }



    @GetMapping("/downloadWorkFlow")
    @ApiOperation(value = "APP下载隐患管理业务流程" )
    public ResultBean downloadWorkFlow(){
        try {

            WorkFlowProcess workFlowProcess = workFlowProcessService.findByBizName(WorkFlowConstants.APP_WORKFLOW_HIDDEN_DANGER);

            return ResultBean.success(workFlowProcess);
        } catch (Exception e) {
            log.error("APP下载隐患管理业务流程失败", e);
            throw e;
        }
    }

    @GetMapping("/downloadMenuList")
    @ApiOperation(value = "由APP下载隐患管理菜单配置" )
    public ResultBean downloadMenuList(){
        try {

            WorkFlowProcess workFlowProcess = workFlowProcessService.findByBizName(WorkFlowConstants.APP_WORKFLOW_HIDDEN_DANGER);
            List<WorkFlowMenu> list = workFlowMenuService.getMenuList(workFlowProcess.getId());
            return ResultBean.success(list);
        } catch (Exception e) {
            log.error("APP下载隐患管理菜单配置失败", e);
            throw e;
        }
    }

}
